<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\model\Auth;
use app\admin\validate\RoleAdd;
use app\admin\validate\RoleEdit;
use think\exception\HttpException;
use think\exception\ValidateException;
use think\facade\View;
use think\Request;

class Role extends Base
{
    /**
     * 角色列表展示
     * @param \app\admin\model\Role $role
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function roleList(\app\admin\model\Role $role)
    {
        $data = $role->with('admin')->select()->toArray();
        View::assign('list',$data);
        return View::fetch('admin-role');
    }

    /**
     * 角色添加页面展示
     * @param Auth $auth
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function roleAdd(Auth $auth)
    {
        $auths = fsFlat($auth->select());
        View::assign('auths',$auths);
        return View::fetch('admin-role-add');
    }

    /**
     * 角色信息保存
     * @param \app\admin\model\Role $role
     * @return \think\response\Json|void
     */
    public function roleSave(\app\admin\model\Role $role)
    {
        try {
            $data = request()->all();
            validate(RoleAdd::class)->check($data);
            $data['role_auth_ids'] = implode('_',$data['auth_ids']);
            unset($data['auth_ids']);
            $data['create_time'] = time();
            $data['update_time'] = time();
            $result = $role->insert($data);
            if ($result){
                abort(200,'添加成功');
            }
            abort(2002,'添加失败');
        } catch (HttpException $exception){
            return success($exception->getStatusCode(),$exception->getMessage());
        } catch (ValidateException $exception){
            return success(2001,$exception->getError());
        }
    }

    /**
     * 角色信息编辑页面展示
     * @param \app\admin\model\Role $role
     * @param Auth $auth
     * @param $id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function roleedit(\app\admin\model\Role $role,Auth $auth,$id)
    {
        $data = $role->find($id);
        View::assign('info',$data);
        $auths = fsFlat($auth->select());
        View::assign('auths',$auths);
        return View::fetch('admin-role-edit');
    }

    /**
     * 角色信息保存修改
     * @param \app\admin\model\Role $role
     * @return \think\response\Json|void
     */
    public function roleUpdate(\app\admin\model\Role $role)
    {
        try {
            $data = request()->all();
            validate(RoleEdit::class)->check($data);
            $data['role_auth_ids'] = implode('_',$data['auth_ids']);
            unset($data['auth_ids']);
            $data['update_time'] = time();
            $result = $role->where('id',$data['id'])->update($data);
            if ($result){
                abort(200,'修改成功');
            }
            abort(2002,'修改失败');
        } catch (HttpException $exception){
            return success($exception->getStatusCode(),$exception->getMessage());
        } catch (ValidateException $exception){
            return success(2001,$exception->getError());
        }
    }

    /**
     * 单条角色信息删除
     * @param \app\admin\model\Role $role
     * @param $id
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function roleDelete(\app\admin\model\Role $role,$id)
    {
        try {
            if (!is_numeric($id)){
                abort(2001,'传参错误');
            }
            $data = $role->find($id);
            $data->delete();
            abort(200,'删除成功');
        } catch (HttpException $exception){
            return success($exception->getStatusCode(),$exception->getMessage());
        }
    }

    public function roleBatchDelete(\app\admin\model\Role $role)
    {
        try {
            $ids = request()->param('ids');
            $ids = explode(',',trim($ids));
            $result = $role->whereIn('id',$ids)->select();
            $result->delete();
            abort(200,'删除成功');
        } catch (HttpException $exception){
            return success($exception->getStatusCode(),$exception->getMessage());
        }
    }
}
